//
//  XJAfterSaleHistoryViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/20.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  名字起乱了。 这个的入口是:我的---》我的订单---> 点击 “售后” 按钮


#import "XJAfterSaleHistoryViewController.h"
#import "XJAfterSaleTVC1.h"
#import "XJAfterSaleTVC2.h"

@interface XJAfterSaleHistoryViewController ()<UIScrollViewDelegate>
@property(nonatomic, strong) UIView * titleView;
/**标记选中的按钮*/
@property(nonatomic, strong) UIButton *selectedButton;
/**黄色的线*/
@property(nonatomic, strong) UIView *yellowLine;
/**可滚动的view*/
@property(nonatomic, strong) UIScrollView *scrollView;


@end

@implementation XJAfterSaleHistoryViewController

- (UIView *)yellowLine {
    if (!_yellowLine) {
        _yellowLine = [UIView new];
        _yellowLine.backgroundColor = XJCOLOR_RGB(255, 218, 67);
    }
    return _yellowLine;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"售后";
    
    [self setupTitleView];
    
    [self addChildVCs];
    
    [self setupScrollView];
    
    [self addChildViews];
    
}

#pragma mark - 一 UI
- (void)setupTitleView {
    
    //创建对象
    self.titleView = [UIView new];
    self.titleView.frame = CGRectMake(0, 0, KWidth, 40);
    [self.view addSubview:self.titleView];
    
    NSArray *titleArr = @[@"售后申请",@"申请历史"];
    
    CGFloat btn_w = KWidth / titleArr.count;
    
    for (int i=0 ; i<titleArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.tag = i;
        [button setTitle:titleArr[i] forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [button addTarget:self action:@selector(clickTitleButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        button.frame = CGRectMake(btn_w *i, 0, btn_w, 38);
        [self.titleView addSubview:button];
    }
    
    
    //添加黄线
    UIButton *firstBtn = [self.titleView.subviews firstObject];
    self.yellowLine.rb_height = 2;
    self.yellowLine.rb_width = 50;
    self.yellowLine.rb_y = self.titleView.rb_bottom - 2;
    self.yellowLine.rb_center_x = firstBtn.rb_center_x;
    [self.titleView addSubview:self.yellowLine]; //添加
    
    [self clickTitleButtonAction:firstBtn];
}

- (void)setupScrollView {
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, KWidth, KHeight - 40)];
    [self.view addSubview:self.scrollView];
    
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    
    
    NSInteger count = self.childViewControllers.count;
    //scrollView的contentSize
    self.scrollView.contentSize = CGSizeMake(KWidth*count, 0);
}


#pragma mark - 二 添加子视图控制器 和子视图控制器的view
- (void)addChildVCs {
    XJAfterSaleTVC1 *tvc1 = [[XJAfterSaleTVC1 alloc] init];
    [self addChildViewController:tvc1];
    
    XJAfterSaleTVC2 *tvc2 = [[XJAfterSaleTVC2 alloc] init];
    [self addChildViewController:tvc2];
}

- (void)addChildViews {
    
    //index
    NSInteger index = self.scrollView.contentOffset.x / self.scrollView.rb_width;
    
    UIViewController *vc = self.childViewControllers[index];
    vc.view.frame = self.scrollView.bounds;
    [self.scrollView addSubview:vc.view];
}


- (void)clickTitleButtonAction:(UIButton *)btn {
    XJLog(@"%@",btn.titleLabel.text);
    
    self.selectedButton.selected = NO;
    btn.selected = YES;
    self.selectedButton  = btn;
    
    
    //黄线动画
    [UIView animateWithDuration:0.25 animations:^{
        self.yellowLine.rb_center_x = btn.rb_center_x;
    }];
    
    //让scrollview滚。
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = btn.tag * self.scrollView.rb_width;
    
    [self.scrollView setContentOffset:offset animated:YES];
}


#pragma mark -  三 UIScrollViewDelegate
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [self addChildViews];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //计算下标
    NSInteger index = scrollView.contentOffset.x / self.scrollView.rb_width;
    //取出对应的btn
    UIButton  *btn = self.titleView.subviews[index];
    
    //切换btn 的状态
    [self clickTitleButtonAction:btn];
    
    //切换scrollview
    [self addChildViews];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
